package tests;

import org.gstreamer.Gst;
import org.junit.Assert;
import org.junit.Before;
import org.junit.After;
import org.junit.Test;

import video.Player;
import rss.News;

import java.io.File;
import java.util.*;

/**
 * @author Heitor Meira de Melo.
 */
public class PlayerTest {
	Player p1;
	List<String> files;
	News news;
	
	@Before
	public void setUp() throws Exception {
		files = new ArrayList<String>();
		files.add("Videos/Samples/bailey.flv");
		files.add("Videos/Samples/stinsonAwesome.flv");
		
		news = new News("http://rss.terra.com.br/0,,EI8266,00.xml");
		
		p1 = new Player(files, news);
		
	}
	
	@After
	public void clearUp(){
		p1 = null;
		files = null;
		Gst.quit();
	}
	
	@Test(expected=Exception.class)
	public void testNullList() throws Exception{
		p1 = new Player(null, news);
		p1.kill();
	}
	
	@Test(expected=Exception.class)
	public void testNullNews() throws Exception{
		p1 = new Player(files, null);
		p1.kill();
	}
	
	@Test(expected=Exception.class)
	public void testEmptyList() throws Exception{
		List<String> l = new ArrayList<String>(); 
		p1 = new Player(l, news);
		
		p1.kill();
	}
	
	
	@Test(expected=Exception.class)
	public void testAddVideoNull() throws Exception{
		p1.createPlayer();
		p1.addVideo(null);
		
		p1.kill();
	}
	
	@Test(expected=Exception.class)
	public void testAddVideoNonexistent() throws Exception{
		p1.createPlayer();
		p1.addVideo( new File("Videos/Samples/PagodeiroMacho.flv"));
		
		p1.kill();
	}
	
	public void testSize() throws Exception{
		p1.createPlayer();
		Assert.assertEquals(2, p1.getPlayList().size());
		
		p1.kill();
	}
	
	@Test
	public void testAddVideo() throws Exception{
		p1.createPlayer();
		p1.addVideo( new File("Videos/Samples/bailey.flv"));
		
		Assert.assertEquals(3, p1.getPlayList().size());
		
		p1.kill();
	}
	
	@Test(expected=Exception.class)
	public void testSetNullPlayList() throws Exception{
		p1.createPlayer();
		p1.setPlayList(null);
		
		p1.kill();
	}
	
	@Test
	public void testSetPlayList() throws Exception{
		List<String> l = new ArrayList<String>();
		l.add("Videos/Samples/bailey.flv");
		l.add("Videos/Samples/stinsonAwesome.flv");
		l.add("Videos/Samples/bailey.flv");
		
		p1.createPlayer();
		p1.setPlayList(l);
		Assert.assertEquals(3, p1.getPlayList().size());
		
		p1.kill();
	}
	
	@Test(expected=Exception.class)
	public void testaddNullPlayList() throws Exception{
		p1.createPlayer();
		p1.addPlayList(null);
		
		p1.kill();
	}
	
	@Test
	public void testAddPlayList() throws Exception{
		List<String> l = new ArrayList<String>();
		l.add("Videos/Samples/stinsonAwesome.flv");
		l.add("Videos/Samples/bailey.flv");
		l.add("Videos/Samples/stinsonAwesome.flv");
		
		
		p1.createPlayer();
		p1.addPlayList(l);
		Assert.assertEquals(5, p1.getPlayList().size());
		
		p1.kill();
	}
	
	@Test
	public void testGetPlayList() throws Exception {
		p1.createPlayer();
		Assert.assertEquals(2, p1.getPlayList().size());
		
		p1.kill();
	}
	
	@Test
	public void testPlayVideo() throws Exception{
		p1.createPlayer();
		p1.play();
		Assert.assertTrue(p1.isPlaying());
		
		p1.kill();
	}
	
	@Test(expected=Exception.class)
	public void testExceptionPlay() throws Exception{
		p1.createPlayer();
		p1.play();
		Assert.assertTrue(p1.isPlaying());
		p1.kill();
		p1.play();
	}
	
	@Test(expected=Exception.class)
	public void testExceptionPause() throws Exception{
		p1.createPlayer();
		p1.play();
		Assert.assertTrue(p1.isPlaying());
		p1.kill();
		p1.pause();
	}
	
	@Test(expected=Exception.class)
	public void testExceptionStop() throws Exception{
		p1.createPlayer();
		p1.play();
		Assert.assertTrue(p1.isPlaying());
		p1.kill();
		p1.stop();
	}
	
	@Test(expected=Exception.class)
	public void testExceptionIsPlaying() throws Exception{
		p1.createPlayer();
		p1.play();
		Assert.assertTrue(p1.isPlaying());
		p1.kill();
		p1.isPlaying();
	}
	
	@Test
	public void testPause() throws Exception {
		p1.createPlayer();
		p1.play();
		Assert.assertTrue(p1.isPlaying());
		p1.pause();
		Assert.assertFalse("Is playing", p1.isPlaying());
		
		p1.kill();
	}
	
	@Test
	public void testPlay() throws Exception {
		p1.createPlayer();
		p1.play();
		Assert.assertTrue(p1.isPlaying());
		p1.pause();
		p1.play();
		Assert.assertTrue("Is not playing", p1.isPlaying());
		
		p1.kill();
	}
	
	@Test
	public void testStop() throws Exception {
		p1.createPlayer();
		p1.play();
		Assert.assertTrue(p1.isPlaying());
		p1.stop();
		Assert.assertFalse("Is playing", p1.isPlaying());
		
		p1.kill();
	}
	
	@Test(expected=Exception.class)
	public void testKill() throws Exception{
		p1.createPlayer();
		p1.play();
		Assert.assertTrue(p1.isPlaying());
		p1.kill();
		Assert.assertFalse("Is playing", p1.isPlaying());
	}
	
	@Test
	public void testRemoveAll() throws Exception{
		p1.createPlayer();
		p1.removeAll();
		Assert.assertEquals(0, p1.getPlayList().size());
	}
	
	@Test
	public void testRemove() throws Exception{
		p1.createPlayer();
		p1.remove( new File("Videos/Samples/bailey.flv"));
		
		Assert.assertEquals(1, p1.getPlayList().size());
	}
}
